package java44th.group.controller.admin.admin;

import java44th.group.bean.admin.bo.admin.UpdateBo;
import java44th.group.bean.admin.bo.admin.CreateBo;
import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.bean.admin.vo.admin.CreateVo;
import java44th.group.bean.admin.vo.admin.UpdateVO;
import java44th.group.service.admin.admin.MarketAdminService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

/**
 * @Aurhor: 王可龙
 * @DATE: 2022/10/11 20:42
 * @Package:java44th.group.controller.admin.admin
 * @Description:
 * @Version 1.0
 */
@RestController
@RequestMapping("admin/admin")
public class ListController {

    /**
     * list接口
     *
     */
    @Autowired
    MarketAdminService marketAdminService;
    @RequiresPermissions("*")
    @RequestMapping("list")
    public BaseRespVo list(Integer page, Integer limit, String username,String sort, String order){
        System.out.println(page+":"+limit+":"+sort+":"+order);
        BaseRespVo list = marketAdminService.list(page, limit, username,sort, order);
        return list;
    }

    @RequiresPermissions("*")
    @RequestMapping("create")
    public BaseRespVo<CreateVo> create(@RequestBody CreateBo createBo,HttpServletRequest request) throws ParseException {
        if (createBo.getUsername().length() < 6) {
            return BaseRespVo.error(601, "管理员名称不符合规定");
        }
        if (createBo.getPassword().length() < 6) {
            return BaseRespVo.error(602, "管理员密码长度不能小于6");
        }
        CreateVo createVo = marketAdminService.create(createBo);
        if (createVo == null) {
            return BaseRespVo.administratorRepeats();
        }
        return BaseRespVo.ok(createVo);
    }
    @RequiresPermissions("*")
    @RequestMapping("update")
    public BaseRespVo<UpdateVO> update(@RequestBody UpdateBo updateBo, HttpServletRequest request) {
        if (updateBo.getUsername().length() < 6) {
            return BaseRespVo.error(601, "管理员名称不符合规定");
        }
        if (updateBo.getPassword().length() < 6) {
            return BaseRespVo.error(602, "管理员密码长度不能小于6");
        }
        System.out.println(updateBo);
        UpdateVO updateVo=marketAdminService.update(updateBo);
        return BaseRespVo.ok(updateBo);
    }
    @RequiresPermissions("*")
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody UpdateBo updateBo,HttpServletRequest request) {
        marketAdminService.delete(updateBo);
        return BaseRespVo.ok(null);
    }
}
